﻿using DMSkin.WPF;
using log4net;
using MXComponent.Common.Utility;
using MXComponent.DAL;
using MXComponent.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MXComponent.Views.formula
{
    /// <summary>
    /// formulaPage.xaml 的交互逻辑
    /// </summary>
    public partial class formulaPage : DMSkinSimpleWindow
    {
        int? formulaid = null;
        public static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private formulaDB fdb = new formulaDB();
        private formuladetailDB fddb = new formuladetailDB();
        public formulaPage()
        {
            InitializeComponent();
        }
        public formulaPage(int formulaid)
        {
            InitializeComponent();
            this.formulaid = formulaid;
            formulaData model = fdb.GetModel(formulaid);
            this.txtformulaname.Text = model.formulaname;
            this.txtformuladesc.Text = model.formuladesc;

        }

        private void Btnok_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.txtformulaname.Text))
            {
                MessageBox.Show("配方名称不能为空！");
                this.txtformulaname.Focus();
                return;
            }

            try
            {
                foreach (var item in dgvList.Items)
                {
                    DataRowView dr = (DataRowView)item;

                    DataGridTemplateColumn templeColumn = dgvList.Columns[4] as DataGridTemplateColumn;
                    if (templeColumn == null) return;

                    FrameworkElement element = templeColumn.GetCellContent(item);
                    TextBox txtplanqty = templeColumn.CellTemplate.FindName("txtplanqty", element) as TextBox;
                    if (txtplanqty != null)
                    {
                        if (!Validator.IsNumber(txtplanqty.Text))
                        {
                            MessageBox.Show("设定重量只能是整数类型！");
                            txtplanqty.SelectAll();
                            txtplanqty.Focus();
                            return;
                        }
                    }
                    templeColumn = dgvList.Columns[5] as DataGridTemplateColumn;
                    if (templeColumn == null) return;

                    element = templeColumn.GetCellContent(item);
                    TextBox txterrorqty = templeColumn.CellTemplate.FindName("txterrorqty", element) as TextBox;
                    if (txterrorqty != null)
                    {
                        if (!Validator.IsNumber(txterrorqty.Text))
                        {
                            MessageBox.Show("误差只能是整数类型！");
                            txterrorqty.SelectAll();
                            txterrorqty.Focus();
                            return;
                        }
                    }

                }

                formulaData model = new formulaData();
                model.formulaname = this.txtformulaname.Text.Trim();
                model.formuladesc = this.txtformuladesc.Text.Trim();
                model.active = 1;
                if (null == formulaid)
                {
                    model.creatby = GlobalClass.user.userid;
                    model.createtime = DateTime.Now.ToString();
                    int nformulaid = fdb.AddRecord(model);

                    foreach (var item in dgvList.Items)
                    {
                        DataRowView dr = (DataRowView)item;
                        formuladetailData fdmodel = new formuladetailData();
                        fdmodel.formulaid = nformulaid;
                        fdmodel.warehouseid = Convert.ToInt32(dr["warehouseid"]);

                        DataGridTemplateColumn templeColumn = dgvList.Columns[4] as DataGridTemplateColumn;
                        if (templeColumn == null) return;

                        FrameworkElement element = templeColumn.GetCellContent(item);
                        TextBox txtplanqty = templeColumn.CellTemplate.FindName("txtplanqty", element) as TextBox;
                        if (txtplanqty != null)
                        {
                            if (!Validator.IsNumber(txtplanqty.Text))
                            {
                                MessageBox.Show("设定重量只能是整数类型！");
                                txtplanqty.SelectAll();
                                txtplanqty.Focus();
                                return;
                            }
                            fdmodel.planqty = Convert.ToInt32(txtplanqty.Text);
                        }
                        if(fdmodel.planqty==0)
                        {
                            continue;
                        }
                        templeColumn = dgvList.Columns[5] as DataGridTemplateColumn;
                        if (templeColumn == null) return;

                        element = templeColumn.GetCellContent(item);
                        TextBox txterrorqty = templeColumn.CellTemplate.FindName("txterrorqty", element) as TextBox;
                        if (txterrorqty != null)
                        {
                            if (!Validator.IsNumber(txterrorqty.Text))
                            {
                                MessageBox.Show("误差只能是整数类型！");
                                txterrorqty.SelectAll();
                                txterrorqty.Focus();
                                return;
                            }
                            fdmodel.errorqty = Convert.ToInt32(txterrorqty.Text);
                        }
                        fdmodel.creatby = GlobalClass.user.userid;
                        fdmodel.createtime = DateTime.Now.ToString();
                        fddb.AddRecord(fdmodel);
                    }
                }
                else
                {
                    model.formulaid = int.Parse(this.formulaid.ToString());
                    model.updateby = GlobalClass.user.userid;
                    model.updatetime = DateTime.Now.ToString();
                    fdb.ModifyRecord(model);
                    fddb.DeleteRecordByformulaid(int.Parse(this.formulaid.ToString()));
                    foreach (var item in dgvList.Items)
                    {
                        DataRowView dr = (DataRowView)item;
                        formuladetailData fdmodel = new formuladetailData();
                        fdmodel.formulaid = int.Parse(this.formulaid.ToString());
                        fdmodel.warehouseid = Convert.ToInt32(dr["warehouseid"]);
                       
                        DataGridTemplateColumn templeColumn = dgvList.Columns[4] as DataGridTemplateColumn;
                        if (templeColumn == null) return;

                        FrameworkElement element = templeColumn.GetCellContent(item);
                        TextBox txtplanqty = templeColumn.CellTemplate.FindName("txtplanqty", element) as TextBox;
                        if (txtplanqty != null)
                        {
                            if (!Validator.IsNumber(txtplanqty.Text))
                            {
                                MessageBox.Show("设定重量只能是整数类型！");
                                txtplanqty.SelectAll();
                                txtplanqty.Focus();
                                return;
                            }
                            fdmodel.planqty = Convert.ToInt32(txtplanqty.Text);
                        }
                        if (fdmodel.planqty == 0)
                        {
                            continue;
                        }
                        templeColumn = dgvList.Columns[5] as DataGridTemplateColumn;
                        if (templeColumn == null) return;

                        element = templeColumn.GetCellContent(item);
                        TextBox txterrorqty = templeColumn.CellTemplate.FindName("txterrorqty", element) as TextBox;
                        if (txterrorqty != null)
                        {
                            if (!Validator.IsNumber(txterrorqty.Text))
                            {
                                MessageBox.Show("误差只能是整数类型！");
                                txterrorqty.SelectAll();
                                txterrorqty.Focus();
                                return;
                            }
                            fdmodel.errorqty = Convert.ToInt32(txterrorqty.Text);
                        }
                        fdmodel.creatby = GlobalClass.user.userid;
                        fdmodel.createtime = DateTime.Now.ToString();
                        fddb.AddRecord(fdmodel);
                    }
                }
                MessageBox.Show("保存成功!");
                this.DialogResult = true;
            }

            catch (Exception ex)
            {
                Log.Error(ex);
                MessageBox.Show(ex.Message);
            }

            //foreach (DataRowView dr in dgvList.Items)
            //{
            //    if (dr[1].ToString() == "")
            //    {
            //        MessageBox.Show("代码不能为空，请录入！", "ERROR!");
            //        return;
            //    }
            //}
            //for (int i = 0; i < dgvList.Items.Count; i++)
            //{
            //    string value = (dgvList.Columns[1].GetCellContent(dgvList.Items[i]) as TextBlock).Text;
            //    MessageBox.Show(value);

            //    DataGridTemplateColumn templeColumn = dgvList.Columns[4] as DataGridTemplateColumn;
            //    if (templeColumn == null) return;

            //    FrameworkElement element = templeColumn.GetCellContent(dgvList.Items[i]);
            //    TextBox txtplanqty = templeColumn.CellTemplate.FindName("txtplanqty", element) as TextBox;
            //    if (txtplanqty != null)
            //    {
            //        MessageBox.Show(txtplanqty.Text);
            //    }
            //}

            //foreach (var item in dgvList.Items)
            //{
            //    DataRowView dr = (DataRowView)item;
            //    MessageBox.Show(dr[1].ToString());
            //    DataGridTemplateColumn templeColumn = dgvList.Columns[4] as DataGridTemplateColumn;
            //    if (templeColumn == null) return;

            //    FrameworkElement element = templeColumn.GetCellContent(item);
            //    TextBox txtplanqty = templeColumn.CellTemplate.FindName("txtplanqty", element) as TextBox;
            //    if (txtplanqty != null)
            //    {
            //        MessageBox.Show(txtplanqty.Text);
            //    }
            //}

        }

        private void Btnclose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void DMSkinSimpleWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.dgvList.LoadingRow += new EventHandler<DataGridRowEventArgs>(this.dataGridEquipment_LoadingRow);
            BindGrid();
        }
        private void dataGridEquipment_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }

        private void BindGrid()
        {
            try
            {
                DataSet ds = new DataSet();
                if (null == formulaid)
                {
                     ds = fdb.GetAllMaterial();
                }
                else
                {
                    ds = fdb.GetAllMaterialUpdate(formulaid);
                }
                this.dgvList.ItemsSource = ds.Tables[0].DefaultView;
                total();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 只限数字输入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxPreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex re = new Regex("[^0-9.-]+");
            e.Handled = re.IsMatch(e.Text);
        }

        private void txtplanqty_TextChanged(object sender, TextChangedEventArgs e)
        {
            total();
        }

        private void total()
        {
            try
            {
                int total = 0;
                int cha = 0;
                //fdmodel.planqty = Convert.ToInt32(txtplanqty.Text);
                foreach (var item in dgvList.Items)
                {
                    DataRowView dr = (DataRowView)item;

                    DataGridTemplateColumn templeColumn = dgvList.Columns[4] as DataGridTemplateColumn;
                    if (templeColumn == null) return;

                    FrameworkElement element = templeColumn.GetCellContent(item);
                    if (element == null) return;
                    TextBox txtplanqty = templeColumn.CellTemplate.FindName("txtplanqty", element) as TextBox;
                    if (txtplanqty != null)
                    {
                        if (Validator.IsNumber(txtplanqty.Text))
                        {
                            total += Convert.ToInt32(txtplanqty.Text);
                        }                   
                    }
                    templeColumn = dgvList.Columns[5] as DataGridTemplateColumn;
                    if (templeColumn == null) return;

                    element = templeColumn.GetCellContent(item);
                    if (element == null) return;
                    TextBox txterrorqty = templeColumn.CellTemplate.FindName("txterrorqty", element) as TextBox;
                    if (txterrorqty != null)
                    {
                        if (Validator.IsNumber(txterrorqty.Text))
                        {
                            cha += Convert.ToInt32(txterrorqty.Text);
                        }
                       
                    }

                }
                this.lbltotal.Text = total.ToString();
                this.lblcha.Text = cha.ToString();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
    }
}
